var app = getApp();
Page({
	data: {
		errImg: app.globalData.errimg,
		id: '',
		page: 1,
		pages: 0,
		goodsList: [],
		keys: '',
	},
	returntext: function(a) {
		var aaa = '1111'
		return aaa;
	},
	onLoad(options) {
		if (options.id) {
			this.data.id = options.id
		}
		if (options.keys) {
			this.setData({
				keys: options.keys
			})
		}

		// 页面初次加载，请求第一页数据
		this.fetchArticleList(1, true, function(e) {})

	},
	searchIcon(e) { //输入
		this.data.keys = e.detail.value;
	},
	search() {
		if (this.data.keys == "") {
			wx.showToast({
				title: "请输入商品名称",
				icon: "none"
			});
			return false;
		}
		this.setData({
			keys: this.data.keys
		})
		this.fetchArticleList(1, true, function(e) {})
	},
	onReachBottom: function() {
		// 下拉触底，先判断是否有请求正在进行中
		// 以及检查当前请求页数是不是小于数据总页数，如符合条件，则发送请求
		if (!this.loading && this.data.page < this.data.pages) {
			this.data.page += 1
			this.fetchArticleList(this.data.page, false, function(e) {})
		}
	},
	onPullDownRefresh: function() {
		// 上拉刷新
		if (!this.loading) {
			this.data.page = 1
			this.fetchArticleList(this.data.page, true, function() {
				wx.stopPullDownRefresh();
			})
		}
	},
	fetchArticleList(pageNo, override, call) {
		var $th = this;
		$th.loading = true
		// 向后端请求指定页码的数据
		if (override) { //重置list
			$th.data.goodsList = [];
		}
		if (call) {
			var post = {
				categoryId: $th.data.id,
				page: pageNo,
				limit: 50
			}

			if ($th.data.keys) {
				delete post.categoryId
				post.goodsName = $th.data.keys
			}

			app.globalAjax(
				'/api/goods/list',
				"get",
				post,
				function(e) {
					if (e.code == 0) {
						$th.setData({
							goodsList: $th.data.goodsList.concat(e.goodsList.list),
							pages:e.goodsList.totalPage
						})
					} else {
						wx.showToast({
							title: e.msg,
							icon: "none"
						});
					}
				})

			setTimeout(function() {
				$th.loading = false;
				call();
			}, 1000)
		}
	},
	gotourl(s) {

	},
	errImg: function(e) {
		var _errImg = e.target.dataset.errImg;
		var _objImg = "'" + _errImg + "'";
		var _errObj = {};
		_errObj[_errImg] = this.data.errImg;
		this.setData(_errObj); //注意这里的赋值方式...
	}
})
